package com.chenfei.api.filter;

import com.chenfei.api.client.UserClient;
import com.chenfei.api.config.WhitelistConf;
import com.chenfei.api.constant.Constants;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author chenfei
 * @description
 * @since 2018/2/8
 */
@Slf4j
public class LoginTokenFilter extends ZuulFilter{
	private static final String LOGIN_TOKEN = "logintoken";

	@Autowired
	private WhitelistConf whitelistConf;
	@Autowired
	private UserClient userClient;
	@Override
	public Object run() {
		/*log.info("=================================================================================");
		RequestContext cxt = RequestContext.getCurrentContext();
		HttpServletRequest request = cxt.getRequest();
		String requestURI = request.getRequestURI();
		log.info(requestURI);
		if(!this.isAllowedUrl(requestURI)){
			this.doResponse(cxt, Constants.RESP_F001_ILLEGAL_REQUEST);
			return null;
		}
		
		String loginToken = request.getHeader(LOGIN_TOKEN);
		// loginToken为空且不再免登陆列表中
		boolean allowedNotNeedLoginUrl = this.isAllowedNotNeedLoginUrl(requestURI);
		if(StringUtils.isEmpty(loginToken) && !allowedNotNeedLoginUrl) {
			this.doResponse(cxt, Constants.RESP_A001_NOT_LOGIN);	
			return null;
		}
		if(!StringUtils.isEmpty(loginToken)) {
			// 调用会员系统验证这个logintoken是否有效
			String customerId = "";//this.customerClient.getCustomerId(loginToken);
			log.info("loginToken:{},customerId:{}",loginToken,customerId);
			// customerId为空且不再免登陆列表中
			if(StringUtils.isEmpty(customerId) && !allowedNotNeedLoginUrl){
				this.doResponse(cxt, Constants.RESP_A001_NOT_LOGIN);	
				return null;
			}
		}*/
		return null;
	}

	@Override
	public boolean shouldFilter() {
		RequestContext cxt = RequestContext.getCurrentContext();
		HttpServletRequest request = cxt.getRequest();

		String method = request.getMethod();
		log.info("url:{},method:{}",request.getRequestURI(),method);
		return true;
	}

	@Override
	public int filterOrder() {
		return 2;
	}

	@Override
	public String filterType() {
		return "pre";
	}

	private void doResponse(RequestContext cxt,String respStr){
		cxt.setSendZuulResponse(false);
		cxt.setResponseBody(respStr);
		cxt.getResponse().setContentType("utf-8");
		cxt.getResponse().setContentType("application/json;charset=utf-8");
	}
	
	private boolean isAllowedUrl(String requestURI) {
		final List<String> allowedUrl = whitelistConf.getAllowedUrl();
		return checkAllow(allowedUrl,requestURI);
	}
	private boolean isAllowedNotNeedLoginUrl(String requestURI) {
		final List<String> allowedNotNeedLoginUrl = whitelistConf.getAllowedNotNeedLoginUrl();
		return checkAllow(allowedNotNeedLoginUrl,requestURI);
	}

	private boolean checkAllow(List<String> allowUrl,String requestURI){
		boolean ret = false;
		if(allowUrl.contains(requestURI)){
			ret = true;
		}else {
			for(String aurl : allowUrl) {
				if(requestURI.matches(aurl)) {
					ret = true;
					break;
				}
			}
		}
		return ret;
	}

}
